<?php
namespace Leads\Model;

use Zend\Db\Adapter\Adapter;
//use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Sql\Select;

class LeadsTable extends AbstractTableGateway
{
	protected $table = 'leads';

	public function __construct(Adapter $adapter) {
		$this->adapter = $adapter;
	}

	public function fetchAll()
	{
		$resultSet = $this->select(function (Select $select) {
			/*$select->from(array('p' => 'person'))  // base table
    				->join(array('c' => 'contact'),     // join table with alias
    				'p.id = c.person_id');         // join expression*/
			$select->order('id ASC');
		});
		$entities = array();
		foreach ($resultSet as $row) {
			$entity = new Entity\Leads();
			$entity->setId($row->id)
			->setFullname($row->fullname)
			->setCompanyname($row->companyname)
			->setStatus($row->status)
			->setOwner($row->owner);
			
			
			$entities[] = $entity;
			
		}
		
		return $entities;
	}

	public function getLeads($id)
	{
		 $row = $this->select(array('id' => (int) $id))->current();
        if (!$row)
            return false;

        $Leads = new Entity\Leads(array(
                    'id' => $row->id,
                    'fullname' => $row->fullname,
                    'companyname' => $row->companyname,
        			'status' => $row->status,
                ));
        return $Leads;
	}
	
	/*public function getContact($id)
	{
		$row = $this->select(array('id' => (int) $id))->current();
		if (!$row)
			return false;
	
		$Contact = new Entity\Contact(array(
				'id' => $row->id,
				'companyname' => $row->companyname,
				'description' => $row->description,
				'latestactivitydatetime' => $row->latestactivitydatetime,
				'website' => $row->website,
				
		));
		return $Contact;
	}
	*/


/*	
	public function saveLeads(Entity\Person $stickyNote) {
		$data = array(
				'note' => $stickyNote->getNote(),
				'created' => $stickyNote->getCreated(),
		);
	
		$id = (int) $stickyNote->getId();
	
		if ($id == 0) {
			$data['created'] = date("Y-m-d H:i:s");
			if (!$this->insert($data))
				return false;
			return $this->getLastInsertValue();
		}
		elseif ($this->getStickyNote($id)) {
			if (!$this->update($data, array('id' => $id)))
				return false;
			return $id;
		}
		else
			return false;
	}
	
	public function removeStickyNote($id) {
		return $this->delete(array('id' => (int) $id));
	}
	
	}
*/
	
}
?>